package org.taobao.ibatiside.codegen;

import java.util.Set;
import org.taobao.ibatiside.core.genmodel.DataObjectGenModel;
import org.apache.commons.lang.StringUtils;

public class DataObjectGen
{
  protected static String nl;
  public static synchronized DataObjectGen create(String lineSeparator)
  {
    nl = lineSeparator;
    DataObjectGen result = new DataObjectGen();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL + "package ";
  protected final String TEXT_3 = ";";
  protected final String TEXT_4 = NL + "%>" + NL + "import ";
  protected final String TEXT_5 = ";";
  protected final String TEXT_6 = NL + "public class ";
  protected final String TEXT_7 = "{" + NL + "\t";
  protected final String TEXT_8 = NL + "\tprivate ";
  protected final String TEXT_9 = " ";
  protected final String TEXT_10 = ";\t" + NL + "\t";
  protected final String TEXT_11 = NL + "\tpublic void set";
  protected final String TEXT_12 = "(";
  protected final String TEXT_13 = " value){" + NL + "\t\tthis.";
  protected final String TEXT_14 = " = value;" + NL + "\t}" + NL + "\tpublic void ";
  protected final String TEXT_15 = "(){" + NL + "\t\treturn this.";
  protected final String TEXT_16 = ";" + NL + "\t}" + NL + "\t";
  protected final String TEXT_17 = NL + "}" + NL;
  protected final String TEXT_18 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    
DataObjectGenModel model = (DataObjectGenModel)argument;

    stringBuffer.append(TEXT_2);
    stringBuffer.append(model.getPkg());
    stringBuffer.append(TEXT_3);
    
for (String one_import : model.getImports()){
    stringBuffer.append(TEXT_4);
    stringBuffer.append(one_import);
    stringBuffer.append(TEXT_5);
    
}

    stringBuffer.append(TEXT_6);
    stringBuffer.append(model.getName());
    stringBuffer.append(TEXT_7);
    
	Set<String> keys = model.getFields().keySet();
	for (String type: keys){
	String name = model.getFields().get(type);
	
    stringBuffer.append(TEXT_8);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(name);
    stringBuffer.append(TEXT_10);
    }for (String type: keys){
		String name = model.getFields().get(type);
		String cap_name = StringUtils.capitalize(name);
	
    stringBuffer.append(TEXT_11);
    stringBuffer.append(cap_name);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(type);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(name);
    stringBuffer.append(TEXT_14);
    stringBuffer.append("boolean".equals(type) ? "is" : "get");
    stringBuffer.append(cap_name);
    stringBuffer.append(TEXT_15);
    stringBuffer.append(name);
    stringBuffer.append(TEXT_16);
    }	
    stringBuffer.append(TEXT_17);
    stringBuffer.append(TEXT_18);
    return stringBuffer.toString();
  }
}
